package data_api

import (
	"blogx_server/common/res"
	"blogx_server/global"
	"blogx_server/models"
	"blogx_server/models/enum"
	"blogx_server/service/redis_service/redis_site"
	"github.com/gin-gonic/gin"
)

type SumResponse struct {
	FlowCount     int   `json:"flowCount"`     //当日流量
	UserCount     int64 `json:"userCount"`     //用户总数
	ArticleCount  int64 `json:"articleCount"`  //文章总数
	MessageCount  int64 `json:"messageCount"`  //消息总数
	CommentCount  int64 `json:"commentCount"`  //评论总数
	NewLoginCount int64 `json:"newLoginCount"` //今日登录
	NewSignCount  int64 `json:"newSignCount"`  //今日注册
}

func (DataApi) SumView(c *gin.Context) {
	var data SumResponse
	data.FlowCount = redis_site.GetFlow()
	global.DB.Model(models.UserModel{}).Count(&data.UserCount)
	global.DB.Model(models.ArticleModel{}).Where("status = ?", enum.ArticleStatusPublished).Count(&data.ArticleCount)
	global.DB.Model(models.ChatModel{}).Count(&data.MessageCount)
	global.DB.Model(models.CommentModel{}).Count(&data.CommentCount)
	global.DB.Model(models.UserLoginModel{}).Where("date(created_at) = date(now())").Count(&data.NewLoginCount)
	global.DB.Model(models.UserModel{}).Where("date(created_at) = date(now())").Count(&data.NewSignCount)
	res.OkWithData(data, c)
}
